﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DrSoftsFrame.Servers;

namespace PreeRegisterDAL
{
    public class WorkDetailDAL
    {
        /// <summary>
        /// 增加同煤版参数  liujia150812
        /// </summary>
        /// <param name="IFTmkyy"></param>
        /// <param name="workdate"></param>
        /// <returns></returns>
        public static DataSet GetWorkDetailInfo(bool IFTmkyy,string workdate)
        {
            try
            {
                string sqlstr = string.Format(@"--查询排班
                                                select * 
                                                into #opdworkplan 
                                                from opdworkplan
                                                where convert(varchar(10),dstarttime,120)='{0}'
                                                and isnull(nMaxPreengageNum,0)>0 and isnull(nMaxDiagnoseNum,0)>0 

                                                --返回科室
                                                select iDeptID from #opdworkplan
                                                group by iDeptID

                                                --返回排班
                                                select * from #opdworkplan;",workdate);
                //同煤版预约判断  liujia150812
                if (IFTmkyy)
                {
                    sqlstr += string.Format(@"    --返回号源
                                                select iDetailId,iWorkPlanID
                                                ,istatus=
                                                case 
                                                when isnull(bclinicflag,0)=1 OR isnull(bdiagflag,0)=1 or isnull(buseflag,0)=1 then 1
                                                else 0 
                                                end  from opdworkplandetail a
                                                where exists(select 1 from #opdworkplan b where a.iworkplanid=b.iworkplanid)");
                }
                else
                {
                    sqlstr+=string.Format(@"    --返回号源
                                                select iDetailId,iWorkPlanID
                                                ,istatus=
                                                case 
                                                when isnull(bPreRegister,0)=1 OR isnull(bCheck,0)=1 or isnull(bLock,0)=1 then 1 
                                                else 0 
                                                end  from opdworkplandetail a
                                                where exists(select 1 from #opdworkplan b where a.iworkplanid=b.iworkplanid)");
                }
                ClinicDb mydb = new ClinicDb();
                DataSet ds = mydb.ExecuteSelect(sqlstr);
                if (ds == null || ds.Tables.Count < 1)
                {
                    return null;
                }
                ds.Tables[0].TableName = "DictDept";
                ds.Tables[1].TableName = "OPDWorkPlan";
                ds.Tables[2].TableName = "OPDWorkPlanDetail";
                return ds;

            }
            catch (Exception err)
            {

                throw;
            }
        }
    }
}
